package com.example.test_witns.ui.home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowInsets;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.NavOptions;
import androidx.navigation.fragment.NavHostFragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.test_witns.R;
import com.example.test_witns.adapter.viewPageAdapter.ScreenSlidePagerAdapter;
import com.example.test_witns.databinding.FragmentHomeBinding;
import com.example.test_witns.ui.post_blog.PostBlogActivity;
import com.example.test_witns.utils.MyInfosHelper;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        MyInfosHelper myInfosHelper = new MyInfosHelper(getContext());

        ViewPager2 viewPager = root.findViewById(R.id.home_view_pager);

        // 设置适配器
        ScreenSlidePagerAdapter adapter = new ScreenSlidePagerAdapter(getActivity());
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(1);

        viewPager.setCurrentItem(1, false);

        TextView view_page_indicator_hot = root.findViewById(R.id.view_page_indicator_hot);
        TextView view_page_indicator_subscribe = root.findViewById(R.id.view_page_indicator_subscribe);
        TextView view_page_indicator_latest = root.findViewById(R.id.view_page_indicator_latest);
        TextView view_page_indicator_interview_skills = root.findViewById(R.id.view_page_indicator_interview_skills);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position){
                    case 1 : view_page_indicator_hot.setTextColor(Color.parseColor("#09C0F5"));
                    view_page_indicator_subscribe.setTextColor(Color.parseColor("#666666"));
                    view_page_indicator_latest.setTextColor(Color.parseColor("#666666"));
                    view_page_indicator_interview_skills.setTextColor(Color.parseColor("#666666"));
                    break;
                    case 0 : view_page_indicator_hot.setTextColor(Color.parseColor("#666666"));
                    view_page_indicator_subscribe.setTextColor(Color.parseColor("#09C0F5"));
                    view_page_indicator_latest.setTextColor(Color.parseColor("#666666"));
                    view_page_indicator_interview_skills.setTextColor(Color.parseColor("#666666"));
                    break;
                    case 2 : view_page_indicator_hot.setTextColor(Color.parseColor("#666666"));
                    view_page_indicator_subscribe.setTextColor(Color.parseColor("#666666"));
                    view_page_indicator_latest.setTextColor(Color.parseColor("#09C0F5"));
                    view_page_indicator_interview_skills.setTextColor(Color.parseColor("#666666"));
                    break;
                    case 3 : view_page_indicator_hot.setTextColor(Color.parseColor("#666666"));
                    view_page_indicator_subscribe.setTextColor(Color.parseColor("#666666"));
                    view_page_indicator_latest.setTextColor(Color.parseColor("#666666"));
                    view_page_indicator_interview_skills.setTextColor(Color.parseColor("#09C0F5"));
                    break;
                }
            }
        });
        view_page_indicator_hot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
            }
        });
        view_page_indicator_subscribe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
            }
        });
        view_page_indicator_latest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2);
            }
        });
        view_page_indicator_interview_skills.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(3);
            }
        });

        NavController navController = NavHostFragment.findNavController(this);

        View post_blog = root.findViewById(R.id.post_blog);
        post_blog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myInfosHelper.checkIfLogin()) {
                    Intent PostBlogNavigationView = new Intent(getContext(), PostBlogActivity.class);
                    startActivity(PostBlogNavigationView);
                }else {
                    toast_Prohibited_Style("登录后方可发布内容");
                }
            }
        });

        Window window = getActivity().getWindow();
        window.setStatusBarColor(Color.parseColor("#eeeeee"));

        NavOptions navOptions = new NavOptions.Builder()
                .setLaunchSingleTop(false) // 确保如果目标 Fragment 已经在顶部，不会创建新的实例
                .setPopUpTo(R.id.navigation_home, true) // 设置为 true 将移除直到起始目的地的所有 Fragment
                .setRestoreState(false) // 不恢复状态，通常与 setPopUpTo 结合使用
                .build();

        TextView textView_analysis = root.findViewById(R.id.Home_analysis);
        textView_analysis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navController.navigate(R.id.action_navigation_home_to_navigation_analysis,null, navOptions);
            }
        });

        TextView textView_todo = root.findViewById(R.id.Home_TODO);
        textView_todo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navController.navigate(R.id.action_navigation_home_to_navigation_todo,null, navOptions);
            }
        });

        return root;
    }

    public void showToastAboveView(View view, Context context, String message) {
        // 创建Toast
        View customView = LayoutInflater.from(context).inflate(R.layout.custom_toast_confirm_layout, null);
        TextView toastText = customView.findViewById(R.id.toast_message);
        toastText.setText(message);

        Toast toast = new Toast(context);
        toast.setView(customView);
        toast.setDuration(Toast.LENGTH_SHORT);

        // 获取控件的位置（相对于屏幕）
        int[] location = new int[2];
        view.getLocationOnScreen(location); // 获取控件在屏幕上的位置
        int viewX = location[0]; // 控件的X坐标
        int viewY = location[1]; // 控件的Y坐标

        WindowInsets insets = getActivity().getWindow().getDecorView().getRootWindowInsets();
        int cutoutHeight = 0;
        if (insets != null) {
            cutoutHeight = insets.getSystemWindowInsetTop(); // 获取刘海高度
        }

        // 计算Toast的偏移量
        int xOffset = 0; // 水平偏移量，与控件的X坐标对齐
        int yOffset = viewY - cutoutHeight; // 垂直偏移量，让Toast显示在控件的顶部

        // 设置Toast的位置
        toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, xOffset, yOffset);

        // 显示Toast
        toast.show();
    }

    public void showProhibitedStyleToastAboveView(View view, Context context, String message) {
        // 创建Toast
        View customView = LayoutInflater.from(context).inflate(R.layout.custom_toast_prohibited_layout, null);
        TextView toastText = customView.findViewById(R.id.toast_message);
        toastText.setText(message);

        Toast toast = new Toast(context);
        toast.setView(customView);
        toast.setDuration(Toast.LENGTH_SHORT);

        // 获取控件的位置（相对于屏幕）
        int[] location = new int[2];
        view.getLocationOnScreen(location); // 获取控件在屏幕上的位置
        int viewX = location[0]; // 控件的X坐标
        int viewY = location[1]; // 控件的Y坐标

        WindowInsets insets = getActivity().getWindow().getDecorView().getRootWindowInsets();
        int cutoutHeight = 0;
        if (insets != null) {
            cutoutHeight = insets.getSystemWindowInsetTop(); // 获取刘海高度
        }

        // 计算Toast的偏移量
        int xOffset = 0; // 水平偏移量，与控件的X坐标对齐
        int yOffset = viewY - cutoutHeight; // 垂直偏移量，让Toast显示在控件的顶部

        // 设置Toast的位置
        toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, xOffset, yOffset);

        // 显示Toast
        toast.show();
    }

    private void toast_Confirm_Style(String message) {
        View customView = LayoutInflater.from(getContext()).inflate(R.layout.custom_toast_confirm_layout, null);
        TextView toastText = customView.findViewById(R.id.toast_message);
        toastText.setText(message);

        Toast toast = new Toast(getContext());
        toast.setView(customView);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP, 0, 40);
        toast.show();
    }

    private void toast_Prohibited_Style(String message) {
        View customView = LayoutInflater.from(getContext()).inflate(R.layout.custom_toast_prohibited_layout, null);
        TextView toastText = customView.findViewById(R.id.toast_message);
        toastText.setText(message);

        Toast toast = new Toast(getContext());
        toast.setView(customView);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP, 0, 40); // 设置在屏幕顶部
        toast.show();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}